package com.frxuexi.mdm2205

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.recyclerview.widget.RecyclerView
import coil.compose.AsyncImage
import com.frxuexi.apps.AppsPage
import com.frxuexi.mdm2205.demo.Demo01
import com.frxuexi.mdm2205.demo.Demo02
import com.frxuexi.mdm2205.demo.Demo03
import com.frxuexi.mdm2205.demo.Demo05
import com.frxuexi.mdm2205.demo.Demo06
import com.frxuexi.mdm2205.demo.Demo07
import com.frxuexi.mdm2205.myview.MyProView
import com.frxuexi.mdm2205.ui.theme.MDM2205Theme
import com.frxuexi.mdm2205.view.MainPage
import com.frxuexi.mdm2205.view.WelcomePage
import com.frxuexi.mymvi.NewsPage
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import okhttp3.internal.wait
lateinit var rv:RecyclerView
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MDM2205Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
//                    var num=5/0
//                    AppsPage()
                    var startPage="welcome"
                    val mmkv = MMKV.defaultMMKV()
                    val navController = rememberNavController()
                    //读取是否进入过app
                    val isIn = mmkv.decodeBool("isIn", false)
                    if(isIn){
                        startPage="main"
                    }else{
                        lifecycleScope.launch {
                            async { delay(5000) }.await()
                            navController.navigate("main")
                            //存储进入状态
                            mmkv.encode("isIn",true)
                        }
                    }
                    NavHost(navController = navController, startDestination = startPage, builder = {
                        composable("welcome"){ WelcomePage(navController)}
                        composable("main"){ MainPage() }
                    })

                    LaunchedEffect(key1 = "", block ={
                        launch {
                            /*myflow().collect{
                                Log.i("===",it.toString())
                            }*/
//                            myflow2()
                            myflow3().filter {
                                it%2==0
                            }.collect{
                                Log.i("===",it.toString())
                            }
                        }
                    } )

                }
            }
        }
    }
}

fun myflow(): Flow<Int> = flow<Int> {
    delay(2000)
    emit(1)
}

suspend fun myflow2(){
    flowOf(3,4,5).collect{
        Log.i("===",it.toString())
    }
}

fun myflow3():Flow<Int>{
    return listOf(8,9,10).asFlow().buffer()
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
}

//@Preview:预览
//@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MDM2205Theme {
        Greeting("World")
    }
}